--- title: Timeseries keywords: fastai sidebar: home_sidebar summary: "Recipes for timeseries" description: "Recipes for timeseries" nb_path: "nbs/03_modeling/timeseries.ipynb" ---
{% raw %}
{% endraw %}

Classic Timeseries Models (SARIMAX, GARCH, etc)

Base Data

  • Classical models tend to make a few assumptions about the data, mostly about stationary data:
    • Constant mean and variance
    • No trend or seasonality
    • Usually normalized
{% raw %}
df = pd.read_csv('https://raw.githubusercontent.com/ritvikmath/Time-Series-Analysis/master/ice_cream_vs_heater.csv', index_col=0, parse_dates=[0])
df.head(3)
heater ice cream
Month
2004-01-01 27 13
2004-02-01 18 15
2004-03-01 14 16
{% endraw %} {% raw %}
fig = px.line(df, x=df.index, y='heater')
{% endraw %} {% raw %}
{% endraw %}

Normalize data

  • Use standard (z) normalization for data that is already normally distributed
  • Use Box Cox normalization for data that is not normally distributed.
{% raw %}
df2 = df.copy()
for col in df2:
    df2[f'boxcox_{col}'] = boxcox(df2[col])[0]
    df2[f'zscore_{col}'] = zscore(df2[col])
    
fig = px.line(df2, y=['heater', 'boxcox_heater', 'zscore_heater'])
{% endraw %} {% raw %}
{% endraw %}

Differencing

{% raw %}
df3 = df2.diff().dropna()
px.line(df3, y=['boxcox_heater'])
{% endraw %}

Make data homoskedastic

{% raw %}
df4 = df3 / df3.groupby(df3.index.year).transform('std')
px.line(df4, y='heater')
{% endraw %}

Remove seasonality

{% raw %}
df5 = df4 / df4.groupby(df4.index.month).transform('mean')
px.line(df5, x=df5.index, y='heater')
{% endraw %}

Prophet

  • Facebook prophet uses STAN under the hood